# coding=utf-8
# author=haishan09@gmail.com
from django import template
from treemenu.models import MenuItem

register = template.Library()

@register.inclusion_tag('sidebar.html', takes_context=True)
def show_side_menu(context, 
                   root_menuitem_pk=None, 
                   show_root_node=False, 
                   active_menuitem_pk=None):
    if root_menuitem_pk != None:
        root_menuitem = MenuItem.objects.get(
            pk=root_menuitem_pk)
    context['root_menuitem'] = root_menuitem
    context['show_root_node'] = show_root_node
    context['active_menuitem_pk'] = active_menuitem_pk
    return context

@register.inclusion_tag('sidebar_menuitem.html', takes_context=True)
def show_side_menuitem(context, menuitem_pk=None, active_menuitem_pk=None):
    if menuitem_pk != None:
        menuitem = MenuItem.objects.get(
            pk=menuitem_pk)
    context['menuitem'] = menuitem
    context['active_menuitem_pk'] = active_menuitem_pk
    context['open'] = menuitem.is_parent(active_menuitem_pk)
    return context

@register.inclusion_tag('topbar.html', takes_context=True)
def show_top_menu(context, root_menuitem_pk=None):
    if root_menuitem_pk != None:
        root_menuitem = MenuItem.objects.get(
            pk=root_menuitem_pk)
    context['root_menuitem'] = root_menuitem
    return context

@register.inclusion_tag('topbar_menuitem.html', takes_context=True)
def show_top_menuitem(context, menuitem_pk=None):
    if menuitem_pk != None:
        menuitem = MenuItem.objects.get(
            pk=menuitem_pk)
    context['menuitem'] = menuitem
    return context
